package com.tianji.promotion.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.utils.StringUtils;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.domain.po.ExchangeCode;
import com.tianji.promotion.domain.query.CodeQuery;
import com.tianji.promotion.domain.vo.CodePageVO;
import com.tianji.promotion.enums.ExchangeCodeStatus;
import com.tianji.promotion.mapper.ExchangeCodeMapper;
import com.tianji.promotion.service.IExchangeCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.promotion.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

import static com.tianji.common.constants.CacheConstant.EXCHANGE_CODE;

/**
 * <p>
 * 兑换码 服务实现类
 * </p>
 *
 * @author niu
 */
@Service
public class ExchangeCodeServiceImpl extends ServiceImpl<ExchangeCodeMapper, ExchangeCode> implements IExchangeCodeService {


    @Autowired
    private Executor executor;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public void asyncGenerateCode(Long couponId, LocalDateTime issueEndTime, Integer totalNum) {//1000
        executor.execute(()->{
            List<ExchangeCode> codeList = new ArrayList<>(totalNum);
            // 从redis中取当前的序列号的值 1000
            // 直接incrby totalNum
            Long maxNo = redisTemplate.opsForValue().increment(EXCHANGE_CODE, totalNum);
            for(int i = maxNo.intValue(); i>=(maxNo-totalNum + 1); i--){
                String code = CodeUtil.generateCode(i, i);
                ExchangeCode exchangeCode = new ExchangeCode();
                exchangeCode.setCode(code);
                exchangeCode.setType(1);
                exchangeCode.setStatus(ExchangeCodeStatus.UNUSED);
                exchangeCode.setExpiredTime(issueEndTime);
                exchangeCode.setExchangeTargetId(couponId);
                exchangeCode.setCreateTime(LocalDateTime.now());
                exchangeCode.setUpdateTime(LocalDateTime.now());
                codeList.add(exchangeCode);
            }
            this.saveBatch(codeList);
        });
    }

    @Override
    public PageDTO<CodePageVO> pageCodes(CodeQuery query) {
        Page<ExchangeCode> page = query.toMpPage("create_time", false);
        this.page(page,
                new LambdaQueryWrapper<ExchangeCode>()
                        .eq(query.getCouponId() != null,
                                ExchangeCode::getExchangeTargetId,
                                query.getCouponId()));
        List<ExchangeCode> records = page.getRecords();
        List<CodePageVO> vos = BeanUtil.copyToList(records, CodePageVO.class);
        PageDTO<CodePageVO> codePageVOPageDTO = new PageDTO<>();
        codePageVOPageDTO.setList(vos);
        codePageVOPageDTO.setTotal(page.getTotal());
        return codePageVOPageDTO;
    }
}
